package com.probiz.estore.content.dao.impl;

import java.util.List;

import com.probiz.estore.common.model.content.SystemMessage;
import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.content.dao.SystemMessageDao;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;

/**
 * Dao implementation for SystemMessage.
*/
public class SystemMessageDaoImpl extends HibernateGenericDaoImpl<SystemMessage> implements SystemMessageDao {

	public Integer[] getCountsOfCustomer(Integer currentUserId) {
		int myConsultListCount = this.countByFilter(Consult.FilterHqlName.MY_CONSULT_LIST, currentUserId).intValue();
		Integer[] counts = new Integer[1];
		counts[0] = Integer.valueOf(myConsultListCount);
		return counts;
	}
	
	protected void initFilterHqlMap() {
		filterHqlMap.put(SystemMessage.FilterHqlName.MY_SYSTEM_MESSAGE_LIST, "FROM SystemMessage c WHERE c.appUser.appuserId=? ORDER BY c.createTime DESC");
		filterHqlMap.put(SystemMessage.FilterHqlName.MY_SYSTEM_MESSAGE_STATUS_LIST, "FROM SystemMessage c WHERE c.appUser.appuserId=? AND c.status=?  ORDER BY c.createTime DESC");
	}

	public void readMessage(Integer messageId) {
		SystemMessage message = loadById(messageId);
		message.setStatus((short)SystemMessage.MESSAGE_READ);
		save(message);
	}

	public Long countNeverReadMessage(Integer appUserId) {
		return countByHql("from SystemMessage m where m.appUser.appuserId=? and m.status=?", appUserId,SystemMessage.MESSAGE_NEVER_READ); 
	}

	public List<SystemMessage> readNeverReadMessage(Integer currentUserId) {
		//Criterion expression =Restrictions.and(Restrictions.eq("status", SystemMessage.MESSAGE_NEVER_READ), Restrictions.eq("appUser.appuserId", currentUserId)) ;
		//return createCriteria(Restrictions.eq("appUser.appuserId", currentUserId)).list(); 
		return this.findByHql("from SystemMessage m where m.appUser.appuserId=? and m.status=?", currentUserId, SystemMessage.MESSAGE_NEVER_READ);
	}

}
